package com.zero.demo.jms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

/**
 * @author zengzr
 * @desc
 * @Date 2019/4/28 19:04
 */
@RestController
@RequestMapping("result")
@Log4j2
@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET,RequestMethod.OPTIONS,RequestMethod.POST})
@EnableScheduling
public class ResultController {

    private static String json = "{\n" +
            "    \"data\": [\n" +
            "        {\n" +
            "            \"id\":\"1\",\n" +
            "            \"姓名\": \"测试\",\n" +
            "            \"单位\": \"浙江\",\n" +
            "            \"运动员\": [\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"张三\",\n" +
            "                    \"id\": \"1\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"李四\",                  \n" +
            "                    \"id\": 2\n" +
            "                }\n" +
            "            ],\n" +
            "            \"obj\": {\n" +
            "                \"50\": \"10:00\",\n" +
            "                \"100\": \"20:00\"\n" +
            "            },\n" +
            "            \"泳道\": 4,\n" +
            "            \"名次\": 5\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\":\"2\",\n" +
            "            \"姓名\": \"测试\",\n" +
            "            \"单位\": \"浙江\",\n" +
            "            \"运动员\": [\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"张三\",\n" +
            "                    \"id\": \"1\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"李四\",                  \n" +
            "                    \"id\": 2\n" +
            "                }\n" +
            "            ],\n" +
            "            \"obj\": {\n" +
            "                \"50\": \"10:00\",\n" +
            "                \"100\": \"20:00\"\n" +
            "            },\n" +
            "            \"泳道\": 4,\n" +
            "            \"名次\": 5\n" +
            "        },{\n" +
            "            \"id\":\"3\",\n" +
            "            \"姓名\": \"测试\",\n" +
            "            \"单位\": \"浙江\",\n" +
            "            \"运动员\": [\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"张三\",\n" +
            "                    \"id\": \"1\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"李四\",                  \n" +
            "                    \"id\": 2\n" +
            "                }\n" +
            "            ],\n" +
            "            \"obj\": {\n" +
            "                \"50\": \"10:00\",\n" +
            "                \"100\": \"20:00\"\n" +
            "            },\n" +
            "            \"泳道\": 4,\n" +
            "            \"名次\": 5\n" +
            "        }\n" +
            "    ],\n" +
            "    \"headList\": [\n" +
            "        {\n" +
            "            \"val\": \"名次\",\n" +
            "            \"width\": \"5%\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"val\": \"单位\",\n" +
            "            \"width\": \"20%\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"val\": \"姓名\",\n" +
            "            \"width\": \"20%\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"val\": \"泳道\",\n" +
            "            \"width\": \"20%\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"val\": \"运动员\",\n" +
            "            \"width\": \"20%\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    private static String jsonA = "{\n" +
            "            \"id\":\"1\",\n" +
            "            \"姓名\": \"测试\",\n" +
            "            \"单位\": \"浙江\",\n" +
            "            \"运动员\": [\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"张三\",\n" +
            "                    \"id\": \"1\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"isLink\": true,\n" +
            "                    \"name\": \"李四\",                  \n" +
            "                    \"id\": 2\n" +
            "                }\n" +
            "            ],\n" +
            "            \"obj\": {\n" +
            "                \"50\": \"10:00\",\n" +
            "                \"100\": \"20:00\"\n" +
            "            },\n" +
            "            \"泳道\": 4,\n" +
            "            \"名次\": 5\n" +
            "        }";

    private static Integer i = 5;

    @GetMapping("query")
    //@Cacheable(value = "data",key = "1")
    public JSONObject query(){
        return (JSONObject) JSON.parse(json);
    }
    /*@GetMapping("query/{id}")
    public JSONObject queryOne(@PathVariable Long id){
        Optional<ResultVO> res = resultDao.findById(id);
        return (JSONObject) JSON.parse(res.get().getTemplate());
    }*/
    @Scheduled(fixedRate = 1000 * 5)
    public void reportCurrentTime(){
        i++;
        JSONObject jsonObject = JSON.parseObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        jsonArray.getJSONObject(0).put("名次",i);
        if (i % 5 == 0){
            JSONObject object = JSON.parseObject(jsonA);
            object.put("id",i);
            jsonArray.add(object);
            jsonObject.put("data",jsonArray);
        }
        json = jsonObject.toJSONString();
    }

    private SimpleDateFormat dateFormat(){
        return new SimpleDateFormat ("HH:mm:ss");
    }
}
